/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseProsecutionRegisterMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseProsecutionRegister;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseProsecutionRegisterService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseProsecutionFileVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseProsecutionRegisterVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 描述: 案件诉讼登记表 case_prosecution_register 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月01日
 */
@Service
public class CaseProsecutionRegisterServiceImpl extends MyBatisServiceSupport implements ICaseProsecutionRegisterService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseProsecutionRegisterServiceImpl.class);

    @Resource
    private CaseProsecutionRegisterMapper caseProsecutionRegisterMapper;

    /**
     * 获取案件诉讼登记信息
     *
     * @param id 诉讼登记唯一标识
     * @author lilihao
     * @date 2022/12/5 0:13
     */
    @Override
    public CaseProsecutionRegister getInfoById(String id) {
        return caseProsecutionRegisterMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取案件诉讼登记信息
     *
     * @param caseProsecutionRegister 诉讼登记信息
     * @author lilihao
     * @date 2022/12/5 0:13
     */
    @Override
    public CaseProsecutionRegisterVo getInfo(CaseProsecutionRegister caseProsecutionRegister) {
        Example example = new Example(CaseProsecutionRegister.class);
        example.createCriteria()
                .andEqualTo("caseId", caseProsecutionRegister.getCaseId())
                .andEqualTo("documentNumber", caseProsecutionRegister.getDocumentNumber())
                .andEqualTo("isDeleted", Constant.STRING_0)
                .andEqualTo("trialState", caseProsecutionRegister.getTrialState());
        List<CaseProsecutionRegister> list = caseProsecutionRegisterMapper.selectByExample(example);
        if (CollectionUtil.isNotEmpty(list)) {
            CaseProsecutionRegisterVo caseProsecutionRegisterVo = BeanUtil.toBean(list.get(0), CaseProsecutionRegisterVo.class);
            if (StrUtil.isNotBlank(list.get(0).getLitigationFile())) {
                JSONArray objects = JSONUtil.parseArray(list.get(0).getLitigationFile());
                List<CaseProsecutionFileVo> fileList = JSONUtil.toList(objects, CaseProsecutionFileVo.class);
                caseProsecutionRegisterVo.setFileList(fileList);
            }
            return caseProsecutionRegisterVo;
        } else {
            return null;
        }
    }

    /**
     * 保存案件诉讼登记信息
     *
     * @param caseProsecutionRegister 诉讼登记信息
     * @author lilihao
     * @date 2022/12/5 0:13
     */
    @Override
    public void saveInfo(CaseProsecutionRegister caseProsecutionRegister) {
        caseProsecutionRegister.setId(BaseUtil.createUid());
        caseProsecutionRegister.setCreateTime(new Date());
        caseProsecutionRegister.setIsDeleted(Constant.STRING_0);
        caseProsecutionRegister.setCreateUserId(String.valueOf(UserUtil.getUserInfo().getUserId()));
        caseProsecutionRegisterMapper.insert(caseProsecutionRegister);
    }

    /**
     * 更新案件诉讼登记信息
     *
     * @param caseProsecutionRegister 诉讼登记信息
     * @author lilihao
     * @date 2022/12/5 0:13
     */
    @Override
    public void updateInfo(CaseProsecutionRegister caseProsecutionRegister) {
        Example example = new Example(CaseProsecutionRegister.class);
        example.createCriteria()
                .andEqualTo("id", caseProsecutionRegister.getId());
        caseProsecutionRegisterMapper.updateByExampleSelective(caseProsecutionRegister, example);
    }

    /**
     * 删除案件诉讼登记信息
     *
     * @param caseProsecutionRegister 诉讼登记信息
     * @author lilihao
     * @date 2022/12/5 0:13
     */
    @Override
    public void delInfo(CaseProsecutionRegister caseProsecutionRegister) {
        Example example = new Example(CaseProsecutionRegister.class);
        example.createCriteria()
                .andEqualTo("caseId", caseProsecutionRegister.getCaseId())
                .andEqualTo("documentNumber", caseProsecutionRegister.getDocumentNumber())
                .andEqualTo("isDeleted", Constant.STRING_0)
                .andEqualTo("trialState", caseProsecutionRegister.getTrialState());
        CaseProsecutionRegister prosecutionRegister = CaseProsecutionRegister.builder().isDeleted(Constant.STRING_1).build();
        caseProsecutionRegisterMapper.updateByExampleSelective(prosecutionRegister, example);
    }
}